package com.zyht.bank.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wx.dao.WxFansUserMapper;
import com.zyht.bank.dao.WxFansVOMapper;
import com.zyht.bank.dao.WxTagsMapper;
import com.zyht.bank.dao.WxTagsSQLMapper;
import com.zyht.bank.entity.WxTags;
import com.zyht.bank.entity.WxTagsExample;
import com.zyht.bank.service.WxTagsService;
import com.zyht.bank.vo.WxFansTagsVO;
import com.zyht.bank.vo.WxFansVO;
import com.zyht.common.BaseDao;
import com.zyht.common.BaseServiceImpl;
import com.zyht.common.util.CommonUtils;

@Service("wxTagsService")
public class WxTagsServiceImpl extends BaseServiceImpl<WxTags, WxTagsExample> implements WxTagsService {

	@Resource
	private WxTagsMapper wxTagsMapper;
	
	@Resource
	private WxFansUserMapper wxFansUserMapper;
	
	@Resource
	private WxFansVOMapper wxFansVOMapper;
	
	@Resource
	private WxTagsSQLMapper wxTagsSQLMapper;
	
	@Override
	public BaseDao<WxTags, WxTagsExample> dao(){

		return wxTagsMapper;
	}

	@Override
	public List<WxFansVO> getFansInfo(String nickName){

		return this.wxFansVOMapper.selectWxFans(nickName);
	}

	@Override
	public List<String> getOpenIds(List<Integer> fanIds){

		return this.wxFansVOMapper.selectOpenid(fanIds);
	}

	@Override
	public String queryByTagIds(List<String> tagIds) {
		return wxTagsMapper.queryByTagIds(tagIds);
	}

	@Override
	public WxTags queryByTagId(String str) {
		return wxTagsMapper.queryByTagId(str);
	}

	@Override
	public String queryByIds(List<String> ids) {
		return wxTagsMapper.queryByIds(ids);
	}

	@Override
	public void updateTagsInfo(WxTags info, String localTag) {
		//更新 wx_tags
		wxTagsMapper.updateByPrimaryKeySelective(info);
		
		//更新 wx_fans_user
		//查询该标签下所有粉丝
		List<WxFansTagsVO> fansUserList = wxTagsMapper.getUserListByLocalTag(localTag);
		if (null == fansUserList || fansUserList.size() ==0)
			return ;
		//更新操作
		for (WxFansTagsVO wxFansTagsVO : fansUserList) {
			Integer id = wxFansTagsVO.getId();
			String localTags = wxFansTagsVO.getLocalTags();
			String wxTags = wxFansTagsVO.getTags();
			if (localTags.contains(",")) {
				String[] localTagsArr = localTags.split(",");
				List<String> localTagsList = Arrays.asList(localTagsArr);
				ArrayList<String> arrayList = new ArrayList<>(localTagsList);
				if (arrayList.contains(localTag)) {
					int index = arrayList.indexOf(localTag);
					arrayList.remove(index);
				}
				localTags = StringUtils.join(arrayList, ",");
			} else {
				localTags = null;
			}
			String wxTag = info.getTagId().toString().trim();
			if (null == wxTags || "".equals(wxTags)) {
				wxTags = wxTag;
			} else if (null != wxTags && !wxTags.contains(",")) {
				wxTags = wxTags + "," + wxTag;
			} else {
				String[] wxTagsArr = wxTags.split(",");
				List<String> wxTagsList = Arrays.asList(wxTagsArr);
				if (!wxTagsList.contains(wxTag)) {
					wxTags = StringUtils.join(wxTagsList, ",").concat("," + wxTag);
				}
			}
			wxTagsMapper.updateFansUserTags(id, localTags, wxTags);
		}
	}

	@Override
	public void updateLocalTagsInfo(WxTags info, String localTag, String wxTag) {
		//更新 local_tags
		wxTagsMapper.updateByPrimaryKeySelective(info);
		
		List<WxFansTagsVO> fansUserList = wxTagsMapper.getUserListByWxTag(wxTag);
		if (null == fansUserList || fansUserList.size() ==0)
			return ;
		for (WxFansTagsVO wxFansTagsVO : fansUserList) {
			Integer id = wxFansTagsVO.getId();
			String localTags = wxFansTagsVO.getLocalTags();
			String wxTags = wxFansTagsVO.getTags();
			if (wxTags.contains(",")) {
				String[] wxTagsArr = wxTags.split(",");
				List<String> wxTagsList = Arrays.asList(wxTagsArr);
				ArrayList<String> arrayList = new ArrayList<>(wxTagsList);
				if (arrayList.contains(wxTag)) {
					int index = arrayList.indexOf(wxTag);
					arrayList.remove(index);
				}
				wxTags = StringUtils.join(arrayList, ",");
			} else { 
				wxTags = null;
			}
			if (null == localTags || "".equals(localTags)) {
				localTags = localTag;
			} else if (null != localTags && !localTags.contains(",")) {
				localTags = localTags + "," + localTag;
			} else {
				String[] localTagsArr = localTags.split(",");
				List<String> localTagsList = Arrays.asList(localTagsArr);
				if (!localTagsList.contains(localTag)) {
					localTags = StringUtils.join(localTagsList, ",").concat("," + localTag);
				}
			}
			wxTagsMapper.updateFansUserTags(id, localTags, wxTags);
		}
	}

	@Override
	public String getAllTagsForTree(List<Integer> integerList) {
		WxTagsExample example = new WxTagsExample();
		example.createCriteria();
		List<WxTags> result = wxTagsMapper.selectByExample(example);
		if (CommonUtils.isNotEmpty(result)) {
			JSONArray allArr = new JSONArray();

			JSONArray loArr = new JSONArray();
			long localTopId = System.currentTimeMillis();
			JSONArray weArr = new JSONArray();
			long wechatTopId = localTopId + 1;

			for (WxTags info : result) {
				String status = info.getTagStatus();
				JSONObject jsonObject = new JSONObject();
				jsonObject.put("id", info.getId());
				jsonObject.put("name", info.getTagName());
				if (integerList.contains(info.getId())) {
					jsonObject.put("checked", true);
				}
				// 本地
				if ("0".equals(status)) {
					jsonObject.put("pId", localTopId);
					jsonObject.put("tagStatus", 0);
					loArr.add(jsonObject);
				}
				// 微信
				if ("1".equals(status)) {
					jsonObject.put("pId", wechatTopId);
					jsonObject.put("tagStatus", 1);
					weArr.add(jsonObject);
				}
			}

			if (CommonUtils.isNotEmpty(loArr)) {
				JSONObject jsonObject = new JSONObject();
				jsonObject.put("id", localTopId);
				jsonObject.put("pId", 0);
				jsonObject.put("name", "本地标签");
				jsonObject.put("nocheck", "true");
				allArr.add(jsonObject);
				allArr.addAll(loArr);
			}

			if (CommonUtils.isNotEmpty(weArr)) {
				JSONObject jsonObject = new JSONObject();
				jsonObject.put("id", wechatTopId);
				jsonObject.put("pId", 0);
				jsonObject.put("name", "微信标签");
				jsonObject.put("nocheck", "true");
				allArr.add(jsonObject);
				allArr.addAll(weArr);
			}

			return allArr.toString();
		}
		return null;
	}

	@Override
	public List<WxTags> queryByMap(Map<String, Object> map) {
		return wxTagsMapper.queryByMap(map);
	}

	@Override
	public List<WxTags> queryByRoleId(Integer roleId) {
		return wxTagsMapper.queryByRoleId(roleId);
	}

	@Override
	public List<WxTags> queryTagsByRoleId(Map<String, Object> map) {
		return wxTagsMapper.queryTagsByRoleId(map);
	}

	@Override
	public void updateCreateTimeById(Long localId) {
		wxTagsMapper.updateCreateTimeById(localId);
		
	}
	
	@Override
	public void updateBatch(List<WxTags> updateList){
		this.wxTagsSQLMapper.updateBatch(updateList);
	}

	@Override
	public void insertBatch(List<WxTags> insertList){
		this.wxTagsSQLMapper.insertBatch(insertList);
	}

	@Override
	@Transactional
	public void updateTagsCount(String tags, Integer fansCount) {
		wxTagsMapper.updateTagsCount(tags,fansCount);
		
	}

	@Override
	public Integer findCountByTag(Integer tags) {
		return wxTagsMapper.findCountByTag(tags);
	}

	@Override
	public List<WxTags> findList() {
		return wxTagsMapper.findList();
	}

}
